package com.example.perfermance;

import java.util.Random;

public class Test {

    private volatile double l;
    private int nLoops;
    private int[] input;


    public Test(int n) {
        this.nLoops = n;
        this.input = new int[nLoops];
        Random r = new Random();
        for (int i = 0; i < nLoops; i++) {
            input[i] = r.nextInt(30);
        }
    }

    public void doTest(boolean isWarmup) {
        long then = System.currentTimeMillis();
        for (int i = 0; i < nLoops; i++) {
            l = fibImpl(input[i]);
        }
        if (!isWarmup) {
            long now = System.currentTimeMillis();
            System.out.println("cost time :" + (now - then));
        }
    }

    private double fibImpl(int n) {
        if (n == 0) return 0d;
        if (n == 1) return 1d;
        double d = fibImpl(n - 2) + fibImpl(n - 1);
        if (Double.isInfinite(d)) {
            throw new ArithmeticException("Overflow");
        }
        return d;

    }
}
